﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Messenger;
using StyleMVVM.Validation;
using StyleMVVM.ViewModel;
using StyleMVVM.View;

namespace ExampleApp.ViewModels.Validation
{
	public class BasicValidationViewModel : PageViewModel, IValidationStateChangedHandler
	{
		private string prefix;
		private string firstName;
		private string lastName;
		private string middleName;
		private string emailAddress;
		private DelegateCommand saveCommand;

		public DelegateCommand SaveCommand
		{
			get
			{
				if (saveCommand == null)
				{
					saveCommand = new DelegateCommand(SaveMethod,
						x => ValidationContext.State == ValidationState.Valid);
				}
				return saveCommand;
			}
		}

		public IEnumerable<string> Prefixes
		{
			get { return new []{ "Mr","Mrs","Ms","MD","Phd"};}
		}
		
		[Required]
		public string Prefix
		{
			get { return prefix; }
			set { SetProperty(ref prefix, value); }
		}

		[Required]
		public string FirstName
		{
			get { return firstName; }
			set { SetProperty(ref firstName, value); }
		}

		[Required]
		public string LastName
		{
			get { return lastName; }
			set { SetProperty(ref lastName, value); }
		}

		[Required]
		public string EmailAddress
		{
			get { return emailAddress; }
			set { SetProperty(ref emailAddress, value); }
		}
		
		public string MiddleName
		{
			get { return middleName; }
			set { SetProperty(ref middleName, value); }
		}
		
		[Import]
		public IValidationContext ValidationContext { get; private set; }

		[Import]
		public IMessageBoxService MessageBox { get; set; }


		[ActivationComplete]
		public void Activated()
		{
			ValidationContext.RegisterValidationStateChangedHandler(this);
		}

		private void SaveMethod(object parameter)
		{
			MessageBox.Show("Save Clicked");
		}

	
		public void StateChanged(IValidationContext context, ValidationState validationState)
		{
			SaveCommand.RaiseCanExecuteChanged();
		}
	}
}
